package com.cloud.login.controller;

import com.cloud.common.domain.Result;
import com.cloud.entity.AuthBody;
import com.cloud.entity.LoginBody;
import com.cloud.entity.SysLoginLogs;
import com.cloud.enums.LoginDevice;
import com.cloud.login.service.ILoginService;
import com.cloud.utils.SecurityUtils;
import com.cloud.utils.StringUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "登录授权管理")
@RestController
@RequestMapping("/security/login")
public class LoginController {

    @Autowired
    private ILoginService loginService;

    @Operation(summary = "验证码账户登录")
    @PostMapping("/inAuth/userLogin")
    public Result userLogin(@RequestBody LoginBody loginBody) {
        loginBody.setSysCode(SecurityUtils.getSysCode());
        SysLoginLogs loginLogs = new SysLoginLogs();
        loginLogs.setLoginDevice(LoginDevice.PC.getCode());
        return loginService.userLogin(loginBody, loginLogs);
    }

    @Operation(summary = "移动端账户登录")
    @PostMapping("/inAuth/mobileLogin")
    public Result mobileLogin(@RequestBody LoginBody loginBody) {
        loginBody.setSysCode(SecurityUtils.getSysCode());
        SysLoginLogs loginLogs = new SysLoginLogs();
        loginLogs.setLoginDevice(LoginDevice.APP.getCode());
        return loginService.mobileUserLogin(loginBody, loginLogs);
    }

    @Operation(summary = "统一账户登录")
    @PostMapping("/inAuth/sysLogin")
    public Result sysLogin(@RequestBody LoginBody loginBody) {
        loginBody.setSysCode(SecurityUtils.getSysCode());
        SysLoginLogs loginLogs = new SysLoginLogs();
        loginLogs.setLoginDevice(LoginDevice.PC.getCode());
        return loginService.mobileUserLogin(loginBody, loginLogs);
    }

    @Operation(summary = "系统授权登录")
    @PostMapping("/inAuth/authLogin")
    public Result authLogin(@RequestBody AuthBody authBody) {
        if (StringUtils.isNull(authBody.getAuthMode())) {
            return Result.error("操作失败:授权方式不能为空");
        }
        if (StringUtils.isNull(authBody.getAuthType())) {
            return Result.error("操作失败:授权类型不能为空");
        }
        authBody.setSysCode(SecurityUtils.getSysCode());
        SysLoginLogs loginLogs = new SysLoginLogs();
        loginLogs.setLoginDevice(LoginDevice.APP.getCode());
        return loginService.authLogin(authBody, loginLogs);
    }

    @Operation(summary = "根据项目编号获取授权信息")
    @GetMapping("/getAuthPointCode")
    public Result getAuthPointCode(String sysCode) {
        if (StringUtils.isNull(sysCode)) {
            return Result.error("项目编号不能为空");
        }
        return loginService.setLoginPointToken(sysCode);
    }

    @Operation(summary = "根据用户openId获取登录信息")
    @GetMapping("/inAuth/getAuthPointToken")
    public Result getAuthPointToken(String openId) {
        if (StringUtils.isNull(openId)) {
            return Result.error("用户openId不能为空");
        }
        return loginService.getAuthPointToken(openId);
    }

}
